package com.yft.controller.posinformation;

import com.yft.entity.base.Order;
import com.yft.entity.base.OrderSort;
import com.yft.entity.base.Pager;
import com.yft.entity.base.PagerImpl;
import com.yft.entity.posinformation.PosBusinessType;
import com.yft.service.pos_information.PosBusinessTypeService;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;


@Controller
public class PosBusinessTypeController {
	private static final String controllerPath = "/yft/posBusinessType/";
//	private String viewpath = "/admin/base/jsp/PosBusinessTypeManage/";
	private String jsonViewpath = "/admin/pos_information/jsp/json/";
	
	@Autowired(required = true)
	private PosBusinessTypeService posBusinessTypeService;

	private boolean checkNullBlank(Object[] array){
		if(array==null || array.length==0){
			return false;
		}
		for(int i=0;i<array.length;i++){
			if(array[i] != null && !"".equals(((String)array[i]).trim())){
				continue;
			}else{
				return false;
			}
		}
		return true;
	}
	@RequestMapping(controllerPath + "getPosBusinessTypeList.html")
	public String getPosBusinessTypeList(HttpServletRequest request,HttpServletResponse response, ModelMap model) {
		PosBusinessType condition = new PosBusinessType();
		Pager pager = new PagerImpl();
        String query = StringUtils.trimToEmpty(request.getParameter("query"));
		String limit = request.getParameter("limit");
        if(query!=null && query.length()>0)
		    condition.setSe_typeName(query+"%");
		int pageSize = 7;
		if (limit != null && limit.length() > 0)
			pageSize = NumberUtils.toInt(limit) > 0 ? NumberUtils.toInt(limit) : pageSize;
		String start = request.getParameter("start");
		int page = 1;
		if (start != null && start.length() > 0)
			page = NumberUtils.toInt(start) / pageSize + 1;
		pager.setPageSize(pageSize);
		pager.setPage(page);
		Order order = new Order();
		order.addOrder("createTime", OrderSort.DESC);
		
		List<PosBusinessType> posBusinessTypeList =  posBusinessTypeService.getPosBusinessTypeList(condition, order, pager);
		
		model.addAttribute("totalProperty",pager.getRecordCount());
		model.addAttribute("posBusinessTypeList",posBusinessTypeList);
		
		return jsonViewpath + "pos_business_type";
	}
	@RequestMapping(controllerPath + "addPosBusinessType.html")
	public String addPosBusinessType(HttpServletRequest request,HttpServletResponse response, ModelMap model) throws Exception {
		String name = StringUtils.trimToEmpty(request.getParameter("name"));
		String desc = StringUtils.trimToEmpty(request.getParameter("desc"));
		boolean flag = false;
		String error = "";
		if( checkNullBlank(new Object[]{name})){
			PosBusinessType posBusinessType = new PosBusinessType();
            posBusinessType.setTypeName(name);
			if(desc!=null && desc.length()>0){
                posBusinessType.setTypeDesc(desc);
			}
            if(posBusinessTypeService.addPosBusinessType(posBusinessType)!=null){
                flag = true;
            }else{
                error = "添加失败";
            }
		}else{
			error = "请将信息补全";
		}
		model.addAttribute("success", flag);
		model.addAttribute("error", error);
		return "message/messageJson";
	}

    @RequestMapping(controllerPath + "getPosBusinessTypeListForCom.html")
    public String getPosBusinessTypeListForCom(HttpServletRequest request,HttpServletResponse response, ModelMap model) {
        PosBusinessType condition = new PosBusinessType();
        Pager pager = new PagerImpl();
        String query = StringUtils.trimToEmpty(request.getParameter("query"));
        String limit = request.getParameter("limit");
        condition.setSe_typeName(query+"%");
        int pageSize = 7;
        if (limit != null && limit.length() > 0)
            pageSize = NumberUtils.toInt(limit) > 0 ? NumberUtils.toInt(limit) : pageSize;
        String start = request.getParameter("start");
        int page = 1;
        if (start != null && start.length() > 0)
            page = NumberUtils.toInt(start) / pageSize + 1;
        pager.setPageSize(pageSize);
        pager.setPage(page);
        Order order = new Order();
        order.addOrder("createTime", OrderSort.DESC);

        List<PosBusinessType> posBusinessTypeList =  posBusinessTypeService.getPosBusinessTypeList(condition, order, pager);

        model.addAttribute("totalProperty",pager.getRecordCount());
        model.addAttribute("posBusinessTypeList",posBusinessTypeList);

        return jsonViewpath + "pos_business_type";
    }

}
